package io.bdmc.modules.bss.controller;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.bdmc.common.utils.MyMap;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.common.utils.RedisUtils;
import io.bdmc.modules.bss.entity.BarRealtime;
import io.bdmc.modules.bss.model.DevDataParam;
import io.bdmc.modules.bss.service.BarRealtimeService;
import io.netty.util.internal.StringUtil;

/**
 * <p>
 * 实时数据表，每个设备最新一条数据 前端控制器
 * </p>
 *
 * @author HuangLu
 * @since 2019-01-09
 */
@RestController
@RequestMapping("/bss/realtime")
public class BarRealtimeController {

    @Autowired
    private BarRealtimeService realSvs;

    @Autowired
    private RedisUtils _redisSvs;

    @RequestMapping("/list")
    // @RequiresPermissions("sys:dev:list")
    public R list(DevDataParam params) {
        // 查询列表数据
        MyPage<BarRealtime> myPage = new MyPage<BarRealtime>(params.getCurrent(), params.getSize());
        MyPage<BarRealtime> listData = realSvs.mySelectPage(myPage, params);
        return R.ok().putData(MyMap.ok(listData.getRecords(), listData.getTotal()));
    }

    @RequestMapping("/all")
    // @RequiresPermissions("sys:dev:list")
    public R all() {
        // 查询列表数据
        List<BarRealtime> allData = realSvs.allData();
		return R.ok().putData(allData);
    }

    @RequestMapping("/count")
    // @RequiresPermissions("sys:dev:list")
    public R count() {
        // 查询首页统计
		return R.ok().putData( realSvs.devCount());
    }

    @RequestMapping("/now")
    // @RequiresPermissions("sys:dev:list")
    public R now(@RequestBody Map<String,String> req) {
        // 发送实时数据
        if(StringUtil.isNullOrEmpty(req.get("sim"))){
            return R.error("参数错误");
        }
        String sim = req.get("sim").toString();
        _redisSvs.publisher("DownSendDeviceChannel_FG", "8201#"+sim);
		return R.ok();
    }

    @RequestMapping("/alarm")
    // @RequiresPermissions("sys:dev:list")
    public R alarm(DevDataParam params) {
        // 查询列表数据
        params.setIsAlarm(1);
        params.setPositioningState(-1);
        MyPage<BarRealtime> myPage = new MyPage<BarRealtime>(params.getCurrent(), params.getSize());
        MyPage<BarRealtime> listData = realSvs.mySelectPage(myPage, params);
        return R.ok().putData(MyMap.ok(listData.getRecords(), listData.getTotal()));
    }

    @RequestMapping("/cancel_alarm")
    // @RequiresPermissions("sys:dev:list")
    public R cancelAlarm(@RequestBody Map<String,String> req) {
        // 取消报警信息
        if(StringUtil.isNullOrEmpty(req.get("sim"))){
            return R.error("参数错误");
        }
        String sim = req.get("sim").toString();

        QueryWrapper<BarRealtime> w1 = new QueryWrapper<BarRealtime>();
        w1.eq("sim", sim);
        BarRealtime r = realSvs.getOne(w1);
        if(r!=null){
            r.setIsAlarm(false);
            realSvs.saveOrUpdate(r);
        }
		return R.ok();
    }
}